package ooo.reindeer.cedf;



import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import ooo.reindeer.commons.utils.SerialNumberUtil;

import java.io.IOException;


public class EventFast<V> {

    final static ObjectMapper mapper = new ObjectMapper();

    private final String id;
    private final String type;
    private final Object data;

    public String getDataClass() {
        return dataClass;
    }

    private final String dataClass;

    public EventFast(String type) {
        this(type, null);
    }

    public EventFast(String type, Object data) {
        this.id = SerialNumberUtil.getSerialNumber();
        this.type = type;
        this.data = data;
        if (data != null) {
            this.dataClass = data.getClass().getName();
        } else {
            this.dataClass = null;
        }
    }

    public String getId() {
        return id;
    }

    public String getType() {
        return type;
    }

    public <T> T getData() {
        return (T) data;
    }

    public String toString() {
        try {
            return mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public EventFast clone() {
        try {
            return mapper.readValue(mapper.writeValueAsString(this), EventFast.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static EventFast fromString(String json) {
        try {
            return mapper.readValue(json, EventFast.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }



}
